//封装了layer的一些弹窗页面的js
var lui={
	info :function(msg){
		var index=layer.msg('提示：'+msg, {icon: 0,timeout:2000});
		return index;
	},
	success :function(msg){
		var index=layer.msg('成功：'+msg, {icon: 1,timeout:2000});
		return index;
	},
	fail :function(msg){
		var index=layer.msg('失败：'+msg, {icon: 5,timeout:2000});
		return index;
	},
	warn :function(msg){
		var index=layer.msg('警告：'+msg, {icon: 0,timeout:2000});
		return index;
	},
	help :function(msg){
		var index=layer.msg('帮助：'+msg, {icon: 3,timeout:2000});
		return index;
	},
	/**确认,取消**/
	confirm :function(msg,funok,funcancel){
		layer.confirm(msg, {
			btn: ['确认','取消'] //按钮
		}, function(index){
			layer.close(index);
			funok(index);
		}, 
		function(index){
			layer.close(index);
			funcancel(index);
		});
	},
	close :function(index){
		layer.close(index);
	},
	open : function(options){
		var data_options = options["data-options"];
		var defaults ={
			type: 2,
			shadeClose: true,
			fix: false, //不固定
			area: '75%',
			offset: '50px',
			scrollbar: false,
			btn: ['确定', '取消'],
			yes: function(index, layero){ //确认按钮
				if(data_options){
					var $selector = lui.iframe(data_options["selector"],layero);
					if(data_options["yesCallback"]){
						data_options["yesCallback"].call(window,$selector,$(cell),index);
					}
				}
			},
			cancel : function(index){
				if(data_options && data_options["cancelCallback"]){
					data_options["cancelCallback"].call(window,index);
				}
			}
		}
		var options =$.extend(true,{},defaults,options);
		return layer.open(options);
	},
	/** lui.iframe(selector,context);
	 * 获取iframe中的 jQuery对象
	 * @param selector:用来查找的字符串
	 * @param context:作为待查找的 DOM 元素集、文档或 jQuery 对象。
	 * @return 返回一个jQuery对象
	 */
	iframe : function(selector,context){
		if(!context){
			context=document;
		}
		return $(context).find("iframe").eq(0).contents().find(selector);
	},
	//获取父节点的dom对象
	parent :function(selector){
		return $(selector, parent.document);
	},
	//重新调整iframe弹窗的大小
	//selector 为iframe中内容元素的选择器
	resizeIframe :function(selector){
		var $iframe =lui.parent(".layui-layer-iframe[type='iframe']");
		var layer_btn_h = $iframe.find(".layui-layer-btn").outerHeight();
		var layer_tit_h = $iframe.find(".layui-layer-title").outerHeight();
		var layer_cont_h = $(selector).height();
		var layer_cont_outerh = $(selector).outerHeight();
		var layer_iframe_h = layer_btn_h + layer_tit_h + layer_cont_h;
		$iframe.height( layer_iframe_h + "px")
			.find(".layui-layer-content").height(layer_cont_h+"px")
			.find("iframe:first").height(layer_cont_outerh+"px");
		return layer_cont_h;
	},
	//重新调整jqGrid列表的大宽度
	//selector 为iframe中内容元素的选择器
	resizeJqGrid : function(selector,context){
		$(window).off('resize.jqGrid').on('resize.jqGrid', function () {
			setTimeout(function(){
				$(selector).jqGrid("setGridWidth",$(context).width());
				$(".ui-jqgrid-bdiv").width($(context).width()+1);
			},50);
		});
	},
	/**
	 * 如果对象obj存在，返回对象obj
	 * 不存在 返回backObj 或者 "-" (backObj是可选参数)
	 */
	back : function(obj,backObj){
		if(obj){
			return obj;
		}
		if(backObj){
			return backObj;
		}
		return "-";
	},
	backs: function(obj,key,backObj){
		if(obj && obj[key]){
			return obj[key];
		}
		if(backObj){
			return backObj;
		}
		return "-";
	},
	backFirst :function(obj,key){
		if(obj && obj[key]){
			return obj[key];
		}else{
			for(var i in obj){
				return obj[i];
			}
			return "";
		}
	},
	unformat : function(obj,val){
		for (var i in obj){
			if (obj[i] == val){
				return i; 
			}
		}
		return '-1'; 
	},
	setFormByObj : function(rowData,objName,type){
		for(var i in rowData){
			var dom= null;
			if(type == "id"){
				dom= document.getElementById( i ) ;
			}else{
				dom= document.getElementsByName(objName+i )[0] ;
			}
			if(dom){
				switch(dom.nodeName){
					case "INPUT":
						dom.value = rowData[i] ;
						break;
					case "SELECT":
						$(dom).children("option")
							.removeAttr("selected")
							.each(function(e){
								if($(this).attr("value") === rowData[i] ){
									$(this).attr("selected",true);
								}
							});
						break;
					case "TEXTAREA" :
						dom.innerHTML = rowData[i];
						break;
					default :
						break;
				}
			}
		}
	},
    //金额格式化
	fmoney : function(s, n) {
		n = n > 0 && n <= 20 ? n : 2;
		s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
		if (s == "NaN") {
			s = "0.00";
		}
		var l = s.split(".")[0].split("").reverse(), r = s.split(".")[1];
		t = "";
		for (i = 0; i < l.length; i++) {
			t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "" : "");
		}
		return t.split("").reverse().join("") + "." + r;
	},
	//时间戳转换成 字符串
	timestampFormat : function(val, format,defaultReturn){
		if(val){
			return moment(val).format(format || "YYYY-MM-DD HH:mm:ss");
		}else{
			if(defaultReturn){
				return defaultReturn;
			}else{
				return "-";
			}
		}
	},
	//数组转换成字符串 用split分开
	arrayToString : function( arr ,split){
		var spl = split || ",";
		var str = "";
		for(var i = 0, iLen = arr.length ; i < iLen ; i++){
			if( i==0 ){
				str = arr[i];
			}else{
				str += spl + arr[i];
			}
		}
		return str ;
	},
	
	//设置时间范围的 开始时间（仅用于时间范围选择器）
	setRangeStartTime :function(endInput){
		var opt={
			onShow:function(current_time,$input){
				lui.setRangeStart(current_time,endInput,this);
			},
			onChangeDateTime :function(current_time,$input){
				lui.setRangeStart(current_time,endInput,this);
			},
			onSelectDate:function(current_time,$input){
				lui.setSelectStart(current_time,endInput,this);
			},
			onSelectTime:function(current_time,$input){
				lui.setSelectStart(current_time,endInput,this);
			}
		};
		return opt;
	},
	setRangeStart :function(current_time,endInput,start_this){
		var startDate =moment(current_time).format("YYYYMMDD"); 
		var end=$(endInput).val();
		if(end){
			end =end.split(" ");
			endDate =end[0].replace(/-/g,"");
			if(startDate < endDate){
				start_this.setOptions({
					maxDate:end[0],
					maxTime:"24:00"
				});
			}else{
				start_this.setOptions({
					maxDate:end[0],
					maxTime:end[1]
				});
			}
		}
	},
	setSelectStart :function(current_time,endInput,start_this){
		var ct =moment(current_time).format("YYYY-MM-DD HH:mm"); 
		var startStr =ct.replace(/[-: ]*/g,"");
		var end=$(endInput).val();
		if(end){
			endStr =end.replace(/[-: ]*/g,"");
			start_this.setOptions({
				value: (startStr > endStr) ?end:ct
			});
		}
	},

	
	//设置时间范围的 结束时间（仅用于时间范围选择器）
	setRangeEndTime :function(startInput){
		var opt={
				onShow:function(current_time,$input){
					lui.setRangeEnd(current_time,startInput,this);
				},
				onChangeDateTime :function(current_time,$input){
					lui.setRangeEnd(current_time,startInput,this);
				},
				onSelectDate:function(current_time,$input){
					lui.setSelectEnd(current_time,startInput,this);
				},
				onSelectTime:function(current_time,$input){
					lui.setSelectEnd(current_time,startInput,this);
				}
			};
			return opt;
	},
	setRangeEnd :function(current_time,startInput,end_this){
		var endDate =moment(current_time).format("YYYYMMDD"); 
		var start=$(startInput).val();
		if(start){
			start =start.split(" ");
			startDate =start[0].replace(/-/g,"");
			if(endDate <= startDate){
				end_this.setOptions({
					minDate:start[0],
					minTime:start[1]
				});
			}else{
				end_this.setOptions({
					minDate:start[0],
					minTime:"00:00"
				});
			}
		}
	},
	setSelectEnd :function(current_time,startInput,end_this){
		var ct =moment(current_time).format("YYYY-MM-DD HH:mm"); 
		var endStr =ct.replace(/[-: ]*/g,"");
		var start=$(startInput).val();
		if(start){
			startStr =start.replace(/[-: ]*/g,"");
			end_this.setOptions({
				value: (endStr < startStr )?start: ct
			});
		}
	},
	singleRow :function(cellvalue){
		var regex=/[\r\n]/;
		var arr=cellvalue.split(regex);
		return arr[0];
	},
	pageNum : function(_id){
		var pageNum =1;
		if(pageNumObj){
			pageNum = pageNumObj[_id];
		}
		return pageNum?pageNum:1;
	}
	
}